package com.example.diyview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Package com.hc.studyview
 * Created by HuaChao on 2016/6/3.
 */
public class MyView extends View {
    private String Text;
    private int TextSize = 15;
    private int TextColor = Color.BLACK;
    private Paint mpaint;

    //调用方法
    public MyView(Context context) {
        this(context,null);
    }
    //layout中的参数设置
    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    //layout中的参数设置(包含style)
    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MyView);
        Text = array.getString(R.styleable.MyView_text);
        TextColor = array.getColor(R.styleable.MyView_textcolor,TextColor);
        TextSize = array.getDimensionPixelSize(R.styleable.MyView_textSize, pTosp(TextSize));
        //回收
        array.recycle();
        mpaint = new Paint();
        mpaint.setAntiAlias(true); //抗锯齿
        mpaint.setTextSize(TextSize);
        mpaint.setColor(TextColor); ///用于onMeasure中的测量
    }

    private int pTosp(int textSize) {
        return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,textSize,getResources().getDisplayMetrics());
    }

    //用于测量布局宽高
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);   //二进制32位  模式2位 值30位
        int wideMode = MeasureSpec.getMode(widthMeasureSpec);
        int heihtMode = MeasureSpec.getMode(heightMeasureSpec);
//        MeasureSpec.AT_MOST wrapcontent
//        MeasureSpec.EXACTLY 确切值  100dp  match  fill
//        MeasureSpec.UNSPECIFIED 尽可能的大 listview scollview
        int wideth = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if(wideMode == MeasureSpec.AT_MOST)
        {
            Rect rect = new Rect();
            mpaint.getTextBounds(Text,0,Text.length(),rect);
            wideth = rect.width() +getPaddingLeft() + getPaddingRight();
        }
        if(heihtMode == MeasureSpec.AT_MOST)
        {
            Rect rect = new Rect();
            mpaint.getTextBounds(Text,0,Text.length(),rect);
            height = rect.height() + getPaddingTop() + getPaddingBottom();
        }
        setMeasuredDimension(wideth,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint.FontMetricsInt fontMetricsInt = mpaint.getFontMetricsInt();
        int baseline = getHeight()/2 + (fontMetricsInt.bottom - fontMetricsInt.top)/2 - fontMetricsInt.bottom;
        canvas.drawText(Text,getPaddingLeft(),getHeight(),mpaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_UP:;
            case MotionEvent.ACTION_DOWN:;
            case MotionEvent.ACTION_HOVER_MOVE:;
        }
        return super.onTouchEvent(event);
    }
}

